package com.example.kaogong.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.CourseCommentDto;
import com.example.kaogong.sys.pojo.vo.CourseCommentVo;
import com.example.kaogong.sys.service.ICourseCommentService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;

/**
 * <p>
 * 课程讨论 前端控制器
 * </p>
 *
 * @author
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/course-comment")
@Api(tags = "课程评论接口")
@Slf4j
public class CourseCommentController {

    @Autowired
    private ICourseCommentService courseCommentService;

    @ApiOperation(value = "当前登录用户评论")
    @PostMapping("")
    public RespBean addCourseComment(@Valid @RequestBody CourseCommentDto courseCommentDto, Principal principal) {
        if (null == principal)
            return RespBean.error();
        log.info("request:{}", new Gson().toJson(courseCommentDto));
        RespBean respBean = courseCommentService.addCourseComment(courseCommentDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号分页获取所有顶级讨论", notes = "按点赞数降序排列")
    @GetMapping("/top/{courseId}/{current}/{size}")
    public RespBean<Page<CourseCommentVo>> getTopCourseComment(@PathVariable Integer courseId, @PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("courseId:{}, current:{}, size:{}", courseId, current, size);
        RespBean<Page<CourseCommentVo>> respBean = courseCommentService.getCourseComment(current, size, courseId, null);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据评论编号分页获取该评论下的回复", notes = "按点赞数降序排列")
    @GetMapping("/reply/{commentId}/{current}/{size}")
    public RespBean<Page<CourseCommentVo>> getCourseCommentReply(@PathVariable Integer commentId, @PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(commentId, new ParamsErrorException("评论编号不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("commentId:{}, current:{}, size:{}", commentId, current, size);
        RespBean<Page<CourseCommentVo>> respBean = courseCommentService.getCourseComment(current, size, null, commentId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "管理员屏蔽某条评论")
    @PutMapping("/{commentId}")
    public RespBean shieldComment(@PathVariable Integer commentId) {
        Assert.notNull(commentId, new ParamsErrorException("评论编号不能为空"));
        log.info("commentId:{}", commentId);
        RespBean respBean = courseCommentService.shieldComment(commentId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录用户删除自己的一条评论")
    @DeleteMapping("/{commentId}")
    public RespBean deleteComment(@PathVariable Integer commentId, Principal principal) {
        if (null == principal)
            return RespBean.error();
        log.info("commentId:{}", commentId);
        RespBean respBean = courseCommentService.deleteComment(commentId, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录用户点赞一条评论")
    @PostMapping("/like/{commentId}")
    public RespBean likeComment(@PathVariable Integer commentId, Principal principal) {
        if (null == principal)
            return RespBean.error();
        log.info("commentId:{}", commentId);
        RespBean respBean = courseCommentService.likeComment(commentId, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

}
